/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hyk.appcfg.wrapper.prefer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class Preference {

    public static enum AppType {

        INVALID, JAVA, PYTHON
    }
    public static final String FILE_SP = System.getProperty("file.separator");
    public static final String PRFER_SDK_LOCATION = "appengine.sdk.java.location";
    public static final String PRFER_PYTHON_SDK_LOCATION = "appengine.sdk.python.location";
    public static final String PRFER_APPCFG_CMD = "appengine.sdk.java.appcfg.command";
    public static final String PRFER_PYTHON_APPCFG_CMD = "appengine.sdk.python.appcfg.command";
    public static final String PRFER_EMAIL = "appengine.email";
    public static final String PRFER_APPID = "appengine.appid";
    public static final String PRFER_JAPP_LOCATION = "appengine.app.location";
    public static final String PRFER_PYAPP_LOCATION = "appengine.pyapp.location";
    public static final String PRFER_APP_VERSION = "appengine.app.version";
    public static final String PRFER_PROXY_MODE = "appengine.proxy.mode";
    public static final String PRFER_PROXY_INFO = "appengine.proxy";
    public static final String PRFER_USEHTTPS = "appengine.usehttps";
    static String USER_HOME = null;
    static Properties prefer = new Properties();
    public static final int NO_PROXY = 0;
    public static final int HTTP_PROXY = 1;
    public static final int HTTPS_PROXY = 2;

    public static void setProxy(String host, String port) {
        //Preference.isProxyEnable = isProxyEnable;
        prefer.setProperty(PRFER_PROXY_INFO, host + ":" + port);
    }

    public static String getProxy() {
        return prefer.getProperty(PRFER_PROXY_INFO);
    }

    public static void setProxyMode(int mode) {
        //Preference.isProxyEnable = isProxyEnable;
        prefer.setProperty(PRFER_PROXY_MODE, "" + mode);
    }

    public static int getProxyMode() {
        return Integer.valueOf(prefer.getProperty(PRFER_PROXY_MODE, "" + NO_PROXY));
    }

    public static boolean  useHttps() {
        return Boolean.valueOf(prefer.getProperty(PRFER_USEHTTPS, "false"));
    }

    public static void  useHttps(boolean value) {
         prefer.setProperty(PRFER_USEHTTPS, "" + value);
    }

    static class ExitHook extends Thread {

        public void run() {
            if (null != prefer) {
                FileOutputStream os = null;
                try {
                    os = new FileOutputStream(USER_HOME + ".appcfgwrapper");
                    prefer.store(os, "##AppCfgWrapper Preference");
                } catch (IOException ex) {
                    Logger.getLogger(Preference.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    try {
                        os.close();
                    } catch (IOException ex) {
                        Logger.getLogger(Preference.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
    }

    public static String getJavaSDKLocation() {
        return prefer.getProperty(PRFER_SDK_LOCATION);
    }

    public static String getPythonSDKLocation() {
        return prefer.getProperty(PRFER_PYTHON_SDK_LOCATION);
    }

    public static String getJavaSDKAppCfgCmd() {
        return prefer.getProperty(PRFER_APPCFG_CMD);
    }

    public static String getPythonSDKAppCfgCmd() {
        return prefer.getProperty(PRFER_PYTHON_APPCFG_CMD);
    }

    public static String getAppID() {
        return prefer.getProperty(PRFER_APPID);
    }

    public static String getEmail() {
        return prefer.getProperty(PRFER_EMAIL);
    }

    public static String getJavaAppLocation() {
        return prefer.getProperty(PRFER_JAPP_LOCATION);
    }

    public static String getPythonAppLocation() {
        return prefer.getProperty(PRFER_PYAPP_LOCATION);
    }

    public static String getAppVer() {
        return prefer.getProperty(PRFER_APP_VERSION, "1");
    }

    public static void setAppVer(String appID) {
        prefer.setProperty(PRFER_APP_VERSION, appID);
    }

    public static void setAppID(String appID) {
        prefer.setProperty(PRFER_APPID, appID);
    }

    public static void setEmail(String appID) {
        prefer.setProperty(PRFER_EMAIL, appID);
    }

    public static boolean setSDKLocation(String location) {
        if (null != location) {
            String OS_NAME = System.getProperty("os.name").toLowerCase();
            String postFix = "sh";
            if (OS_NAME.contains("win")) {
                postFix = "cmd";
            }
            String toolLib = location + FILE_SP + "bin" + FILE_SP + "appcfg." + postFix;
            String pythonTool = location + FILE_SP + "appcfg.py";
            if (new File(toolLib).exists()) {
                prefer.setProperty(PRFER_SDK_LOCATION, location);
                prefer.setProperty(PRFER_APPCFG_CMD, toolLib);
                return true;
            } else if (new File(pythonTool).exists()) {
                prefer.setProperty(PRFER_PYTHON_SDK_LOCATION, location);
                prefer.setProperty(PRFER_PYTHON_APPCFG_CMD, pythonTool);
                return true;
            }

        }
        return false;
    }

    public static AppType setAppLocation(String location) {
        if (null != location) {
            String appLocation = location;
            if (!appLocation.endsWith("war")) {
                appLocation = appLocation + FILE_SP + "war";
            }
            if (new File(appLocation + FILE_SP + "WEB-INF" + FILE_SP + "appengine-web.xml").exists()) {
                prefer.setProperty(PRFER_JAPP_LOCATION, appLocation);
                return AppType.JAVA;
            }

            if (new File(location + FILE_SP + "app.yaml").exists()) {
                prefer.setProperty(PRFER_PYAPP_LOCATION, location);
                return AppType.PYTHON;
            }

        }
        return AppType.INVALID;
    }

    public static void init() throws IOException {
        USER_HOME = System.getProperty("user.home");
        if (!USER_HOME.endsWith(FILE_SP)) {
            USER_HOME = USER_HOME + FILE_SP;
        }
        File preferencePropFile = new File(USER_HOME + ".appcfgwrapper");
        if (!preferencePropFile.exists()) {
            preferencePropFile.createNewFile();
        } else {
            FileInputStream is = new FileInputStream(preferencePropFile);
            prefer.load(is);
            is.close();
        }
        //use system props overwrite config file's
        Iterator skeys = System.getProperties().keySet().iterator();
        while (skeys.hasNext()) {
            String key = (String) skeys.next();
            //appcfgwrapper's property
            if (key.startsWith("appengine")) {
                prefer.put(key, System.getProperties().get(key));
            }
        }

        Runtime.getRuntime().addShutdownHook(new ExitHook());

    }
}
